<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="700"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="年份">
          <a-select v-decorator="['year', {
            rules: [
              {
                required: true,
                message: '年份不能为空!',
              },
            ],
            }]" placeholder="请选择年份" @change="handleChange">
            <a-select-option v-for="(i,index) in yearList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类别">
          <a-select v-decorator="['typeName', {
            rules: [
              {
                required: true,
                message: '类别不能为空!',
              },
            ],
            }]" placeholder="请选择类别" @change="handleChange">
            <a-select-option v-for="(i,index) in selectList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="煤品">
          <a-input v-decorator="['coalProducts', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入煤品量" suffix="万吨标准煤" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="油品">
          <a-input v-decorator="['oils', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入油品量" suffix="万吨标准煤" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="天然气">
          <a-input v-decorator="['naturalGas', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入天然气量" suffix="万吨标准煤" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="热力">
          <a-input v-decorator="['heatingPower', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入热力量" suffix="万吨标准煤" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="电力">
          <a-input v-decorator="['power', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入电力量" suffix="亿千瓦时" />
        </a-form-item>
      </a-form>
      <div style="text-align: center">
        <a @click="openAdd"><a-icon type="question-circle" style="margin-right: 5px"/>需要多类别新增？</a>
      </div>
    </a-spin>
    <eng-consumption-multi-add-modal ref="modalForm"></eng-consumption-multi-add-modal>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import EngConsumptionMultiAddModal from '@views/dataVisualization/components/engConsumptionMultiAddModal'
import { httpAction, postAction } from '@api/manage'

const date = new Date()

export default {
  name: 'EngConsumptionModal',
  components: {
    EngConsumptionMultiAddModal
  },
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      currentTime: '',
      dialogStyle: {
        top: '300px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      selectList: [
        { name: '能源电力', key: '能源电力' },
        { name: '农业', key: '农业' },
        { name: '工业', key: '工业' },
        { name: '钢铁', key: '钢铁' },
        { name: '石化化工', key: '石化化工' },
        { name: '工业其他', key: '工业其他' },
        { name: '建筑业', key: '建筑业' },
        { name: '交通', key: '交通' }
      ],
      yearList: [
        { name: date.getFullYear(), key: date.getFullYear() },
        { name: date.getFullYear() - 1, key: date.getFullYear() - 1 },
        { name: date.getFullYear() - 2, key: date.getFullYear() - 2 },
        { name: date.getFullYear() - 3, key: date.getFullYear() - 3 }

      ],
      url: {
        list: '/carbon/energyAnalysisConsumptionList/list',
        add: '/carbon/energyAnalysisConsumptionList/add',
        edit: '/carbon/energyAnalysisConsumptionList/edit'
      }
    }
  },
  methods: {
    openAdd() {
      this.close()
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '批量新增'
      this.$refs.modalForm.disableSubmit = false
    },
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'year', 'typeName', 'coalProducts', 'oils', 'naturalGas', 'heatingPower', 'power'
        ))
      })
    },
    handleChange(value) {
      console.log(value)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          httpAction(httpurl, formData, method).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('refresh')
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
            that.$parent.$parent.loadData()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>